package com.yx.changdao.init;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.changdao.common.entity.SysRole;
import com.yx.changdao.common.entity.SysUser;
import com.yx.changdao.common.entity.SysUserRole;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.utils.MD5Utils;
import com.yx.changdao.common.utils.SysConst;
import com.yx.changdao.service.SysRoleService;
import com.yx.changdao.service.SysUserRoleService;
import com.yx.changdao.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 初始化admin用户和角色
 * <p>
 * 初始化超级管理员的账户和角色
 * <p>
 * <br/>
 *
 * @Auther: zhangqi
 * @create 2019/11/7 14:31
 */
@Component
public class InitSysUser {


    /**
     * 系统用户
     */
    @Autowired
    private SysUserService sysUserService;

    /**
     * 系统用户角色
     */
    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 系统角色
     */
    @Autowired
    private SysRoleService sysRoleService;

    public void init() throws AlertException {
        // 查询角色
        SysRole role = this.sysRoleService.getById(SysConst.SYS_ROLE);

        // 初始化角色
        if (Objects.isNull(role)) {
            role = this.sysRoleService.addRole(SysConst.SYS_ROLE, SysConst.SYS_ROLE_NAME);
        }

        // 初始化用户
        SysUser user = this.sysUserService.getUserByUsername(SysConst.SYS_LOGIN_NAME);
        if (Objects.isNull(user)) {
            user = new SysUser();
            // 设置初始密码
            user.setPassword(SysConst.SYS_DEFAULT_PASSWORD);
            // 设置登录名
            user.setUsername(SysConst.SYS_LOGIN_NAME);
            user.setCreateBy("0");
            user.setUpdateBy("0");
            // 设置昵称
            user.setNickname(SysConst.SYS_ROLE_NAME);
            user = this.addUser(user);
        }

        // 定义查询条件
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(SysUserRole::getRoleId, role.getRoleId())
                .eq(SysUserRole::getUserId, user.getUserId());

        // 初始化用户角色
        if (this.sysUserRoleService.count(wrapper) == 0) {
            this.sysUserRoleService.addUserRole(user.getUserId(), role.getRoleId());
        }
    }

    private SysUser addUser(SysUser user) throws AlertException {
        if (StringUtils.isBlank(user.getUsername())
                || StringUtils.isBlank(user.getNickname())) {
            throw new AlertException("账户名不可为空");
        }
        if (0 < sysUserService.count(new QueryWrapper<SysUser>().lambda()
                .eq(SysUser::getUsername, user.getUsername()))) {
            throw new AlertException("账号已存在");
        }
        if (StringUtils.isBlank(user.getPassword())) {  //如果没有密码,那么使用系统默认密码
            user.setPassword(SysConst.SYS_DEFAULT_PASSWORD);
        }
        String salt = MD5Utils.createSalt();
        user.setSalt(salt);
        user.setPassword(MD5Utils.shiroEncryption(user.getPassword(), salt));

        boolean b = sysUserService.save(user);
        if (!b) {
            throw new AlertException("添加用户失败");
        }
        return user;
    }

}
